package com.ssyt.examinationsystem.controller;

import com.ssyt.examinationsystem.entity.RoleInfo;
import com.ssyt.examinationsystem.entity.TeacherInfo;
import com.ssyt.examinationsystem.service.IRoleInfoService;
import com.ssyt.examinationsystem.service.ITeacherInfoService;
import com.ssyt.examinationsystem.service.impl.RoleInfoServiceImpl;
import com.ssyt.examinationsystem.service.impl.TeacherInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

/**
 * Servlet implementation class TeacherInfoController
 */
public class TeacherInfoController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public TeacherInfoController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 创建一个业务对象
        ITeacherInfoService teaService = new TeacherInfoServiceImpl();
        // 创建一个角色业务对象
        IRoleInfoService roleService = new RoleInfoServiceImpl();
        // 接收输出流对象
        PrintWriter out = response.getWriter();
        // 得到地址栏上的参数
        String flag = request.getParameter("flag");
        if ("list".equals(flag)) {
            list(teaService, request, response);
        } else if ("delete".equals(flag)) {
            delete(teaService, request, response, out);
        } else if ("initUpdate".equals(flag)) {
            initUpdate(roleService, teaService, request, response);
        } else if ("update".equals(flag)) {
            update(teaService, request, response, out);
        } else if ("initAdd".equals(flag)) {
            initAdd(roleService, teaService, request, response);
        } else if ("add".equals(flag)) {
            add(teaService, request, response, out);
        } else if ("lead".equals(flag)) {
            lead(request, response);
        }

        out.flush();
        out.close();

    }

    /**
     * 转发到导入教师信息的页面
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void lead(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/views/teacher/teaLead.jsp").forward(request, response);
    }

    /**
     * 添加教师信息的方法
     *
     * @param teaService
     * @param request
     * @param response
     * @param out
     * @throws UnsupportedEncodingException
     */
    private void add(ITeacherInfoService teaService, HttpServletRequest request, HttpServletResponse response,
                     PrintWriter out) throws UnsupportedEncodingException {
        // 获取表单数据
        String teaName = request.getParameter("teaName");
        // 对网页数据进行转码
        teaName = URLDecoder.decode(teaName, "UTF-8");

        String account = request.getParameter("account");
        account = URLDecoder.decode(account, "UTF-8");

        String password = request.getParameter("password");
        password = URLDecoder.decode(password, "UTF-8");

        String roleId = request.getParameter("roleId");
        roleId = URLDecoder.decode(roleId, "UTF-8");
        // 创建一个教师对象，封装属性
        TeacherInfo tea = new TeacherInfo();
        tea.setAccount(account);
        tea.setPassword(password);

        tea.setTeaName(teaName);
        RoleInfo role = new RoleInfo();
        role.setRoleId(Integer.valueOf(roleId));
        tea.setRole(role);
        if (teaService.addTeacherInfo(tea) > 0) {
            out.print("ok");
        }
    }

    /**
     * 初始化添加教师的方法
     *
     * @param roleService
     * @param teaService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void initAdd(IRoleInfoService roleService, ITeacherInfoService teaService, HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        // 获取所有角色信息
        List<RoleInfo> roleList = roleService.getRoleInfos();
        request.setAttribute("roleList", roleList);
        request.getRequestDispatcher("WEB-INF/views/teacher/teaAdd.jsp").forward(request, response);
    }

    /**
     * 修改教师的方法
     *
     * @param teaService
     * @param request
     * @param response
     * @param out
     * @throws UnsupportedEncodingException
     */
    private void update(ITeacherInfoService teaService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) throws UnsupportedEncodingException {
        // 获取表单数据
        String teaName = request.getParameter("teaName");
        // 对网页数据进行转码
        teaName = URLDecoder.decode(teaName, "UTF-8");
        String teaId = request.getParameter("teaId");
        teaId = URLDecoder.decode(teaId, "UTF-8");

        String account = request.getParameter("account");
        account = URLDecoder.decode(account, "UTF-8");

        String password = request.getParameter("password");
        password = URLDecoder.decode(password, "UTF-8");

        String roleId = request.getParameter("roleId");
        roleId = URLDecoder.decode(roleId, "UTF-8");
        // 创建一个教师对象，封装属性
        TeacherInfo tea = new TeacherInfo();
        tea.setAccount(account);
        tea.setPassword(password);
        tea.setTeaId(Integer.valueOf(teaId));
        tea.setTeaName(teaName);
        RoleInfo role = new RoleInfo();
        role.setRoleId(Integer.valueOf(roleId));
        tea.setRole(role);
        if (teaService.updateTeacherInfo(tea) > 0) {
            out.print("ok");
        }
    }

    /**
     * 初始化修改的方法
     *
     * @param roleService
     * @param teaService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void initUpdate(IRoleInfoService roleService, ITeacherInfoService teaService, HttpServletRequest request,
                            HttpServletResponse response) throws ServletException, IOException {
        // 获取教师编号
        String teaId = request.getParameter("teaId");
        // 获取角色编号
        String roleId = request.getParameter("roleId");
        // 调用根据学生编号得到教师对象
        Map<String, Object> tea = teaService.getTeaByStuId(teaId);
        request.setAttribute("tea", tea);

        // 调用根据角色编号查询角色对象的业务方法
        Map<String, Object> role = roleService.getRoleByRoleId(roleId);
        request.setAttribute("role", role);
        // 转发到WEB-INF/views/teacher/teaUpdate.jsp页面中
        request.getRequestDispatcher("WEB-INF/views/teacher/teaUpdate.jsp").forward(request, response);
    }

    /**
     * 根据老师编号删除学生信息
     *
     * @param teaService
     * @param request
     * @param response
     * @param out
     */
    private void delete(ITeacherInfoService teaService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        // 获取教师编号
        String teaId = request.getParameter("teaId");
        // 调用根据老师编号删除学生信息的业务方法
        int row = teaService.deleteTeacherInfo(teaId);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * @param teaService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void list(ITeacherInfoService teaService, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 调用查询所有的业务方法
        List<TeacherInfo> teaList = teaService.getTeacherInfos();
        // 将集合放入请求对象中
        request.setAttribute("teaList", teaList);
        // 转发到WEB-INF/views/teacher/teaList.jsp页面中
        request.getRequestDispatcher("WEB-INF/views/teacher/teaList.jsp").forward(request, response);
    }

}
